package isv

import (
	"encoding/json"
    "dhfshop/models/isv"
	"dhfshop/controllers/base"
	"wxsdk/mp/material"
)

//公众号永久素材管理
type PubMaterialController struct {
	base.BaseController
}

// 新增永久图文素材
func (this *PubMaterialController) AddNews() {
	authorizerAppid := this.GetString("authorizer_appid")
	if len(authorizerAppid) < 3 {
	    this.ServeError("authorizer_appid is invalid")
		return
	}
	newsContent := this.GetString("news_content")
	if len(newsContent) < 3 {
	    this.ServeError("news_content is invalid")
		return
	}
	custAccount, err := isv.GetCustAccountByAppId(authorizerAppid)
	if err != nil {
	    this.ServeError(err.Error())
		return
	}
	news := new(material.News)
	err = json.Unmarshal([]byte(newsContent), news)
	if err != nil {
		this.ServeError(err.Error())
		return
	}
	mediaId, err := material.AddNews(custAccount.AccessToken, news)	
	if err != nil {
		this.ServeError(err.Error())
		return
	}
	rsp := struct {
		MediaId  string `json:"media_id"`
	}{MediaId:mediaId}
	this.Data["json"] = rsp
	this.ServeJSON()
}

//上传图文消息内的图片获取URL
func (this *PubMaterialController) PubUploadImg() {
    authorizerAppid := this.GetString("authorizer_appid")
	if len(authorizerAppid) < 3 {
	    this.ServeError("authorizer_appid is invalid")
		return
	}
	custAccount, err := isv.GetCustAccountByAppId(authorizerAppid)
	if err != nil {
	    this.ServeError(err.Error())
		return
	}
	file, _, err := this.GetFile("wxfile")
	if err != nil {
		this.ServeError(err.Error())
		return
	}
	url, err := material.PubUploadImg(custAccount.AccessToken, file)
	if err != nil {
		this.ServeError(err.Error())
		return
	}
	rsp := struct {
		Url  string `json:"url"`
	}{Url:url}
	this.Data["json"] = rsp
	this.ServeJSON()
}

//新增其他类型永久素材
func (this *PubMaterialController) PubUploadOtherMaterial() {
    authorizerAppid := this.GetString("authorizer_appid")
	if len(authorizerAppid) < 3 {
	    this.ServeError("authorizer_appid is invalid")
		return
	}
	mediaType := this.GetString("media_type")
	if len(mediaType) < 5 {
	    this.ServeError("media_type is invalid")
		return
	}
	custAccount, err := isv.GetCustAccountByAppId(authorizerAppid)
	if err != nil {
	    this.ServeError(err.Error())
		return
	}
	file, _, err := this.GetFile("wxfile")
	if err != nil {
		this.ServeError(err.Error())
		return
	}
	mediaId,url,err := material.PubUploadOtherMaterial(custAccount.AccessToken,mediaType,file)
	if err != nil {
		this.ServeError(err.Error())
		return
	}
	rsp := struct {
	    MediaId  string `json:"media_id"`
		Url      string `json:"url"`
	}{MediaId:mediaId, Url:url}
	this.Data["json"] = rsp
	this.ServeJSON()
}

//下载其他类型永久素材，比如图片
func (this *PubMaterialController) PubDownOtherMaterial() {
    authorizerAppid := this.GetString("authorizer_appid")
	if len(authorizerAppid) < 3 {
	    this.ServeError("authorizer_appid is invalid")
		return
	}
	mediaId := this.GetString("media_id")
	if len(mediaId) < 5 {
	    this.ServeError("media_id is invalid")
		return
	}
	custAccount, err := isv.GetCustAccountByAppId(authorizerAppid)
	if err != nil {
	    this.ServeError(err.Error())
		return
	}
	body, err := material.GetMaterialByMediaId(custAccount.AccessToken,mediaId)
	if err != nil {
		this.ServeError(err.Error())
		return
	}
	this.Ctx.ResponseWriter.Write(body)
}
